<?php
/**
 * 商品分类管理
 * Created by Zhy
 * User: Zhy
 * Date: 2019/4/4
 * Time: 16:42
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Goodscate extends Base
{
    /**
     * 分类首页展示
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if(request()->isAjax()) {
            $params = input('param.');
            $where = [];
            //状态
            if(isset($params['assortment_status']) && $params['assortment_status'] != '') {
                $where['assortment_status'] = $params['assortment_status'];
            }
            $where['assortment_del'] = 1;
            
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods_assortment')->where($where)->count();// 获取总条数
            $lists = Db::name('goods_assortment')
                ->field('assortment_id, assortment_time, assortment_name, assortment_status, assortment_type')
                ->where($where)
                ->page($Nowpage, $limits)
                ->order('assortment_time desc')
                ->select();
            
            foreach($lists as $k => $v) {
                $goods_num = Db::table('up_goods')->where('good_assortment', $v['assortment_id'])->count();
                $lists[$k]['goods_num'] = $goods_num;
            }
            
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        if(session('?assortment_id')){
            session('assortment_id', null);
        }
        return $this->fetch();
    }
    
    /**
     * 添加分类
     * @return mixed|\think\response\Json
     */
    public function cateAdd()
    {
        if(Request::instance()->isPost()) {
            $data = [
                'assortment_name' => trim(input('assortment_name'), ' '),
                'assortment_time' => date('Y-m-d H:i:s'),
                'assortment_type' => input('assortment_type')
            ];
            $res = Db::table('up_goods_assortment')->insert($data);
            if($res) {
                return json(['code' => 200, 'msg' => '添加成功']);
            } else {
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('goodscate/cateadd');
    }
    
    /**
     * 批量删除
     * @return \think\response\Json
     */
    public function cateDel()
    {
        $ids = input('ids');
        if($ids == '') {
            return json(['code' => 100, 'msg' => '请选择要删除的分类']);
        }
        //商品表中的数据
        $res = Db::table('up_goods')->where('good_assortment', 'IN', $ids)->select();
        if($res) {
            return json(['code' => 100, 'msg' => '移除商品可删除']);
        }
        $result = Db::table('up_goods_assortment')->where('assortment_id', 'IN', $ids)->setField('assortment_del', 2);
        if($result) {
            return json(['code' => '200', 'msg' => '删除成功']);
        } else {
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 批量修改状态
     * @return \think\response\Json
     */
    public function cateStatus()
    {
        $ids = input('ids');
        $num = input('num');
        if($ids == '') {
            return json(['code' => 100, 'msg' => '请选择分类']);
        }
        $status = Db::table('up_goods_assortment')->where('assortment_id', 'IN', $ids)->column('assortment_status');
        if($num == 1) {//上架
            if(in_array(1, $status)) {
                return json(['code' => 100, 'msg' => '所选分类中含有上架中分类，请重新选择']);
            }
            $msg = '上架成功';
        } else {//下架
            if(in_array(2, $status)) {
                return json(['code' => 100, 'msg' => '所选分类中含有已下架分类，请重新选择']);
            }
            $msg = '下架成功';
        }
        try {
            Db::table('up_goods_assortment')->where('assortment_id', 'IN', $ids)->setField('assortment_status', $num);
            //上下架所属商品
            Db::table('up_goods')->where('good_assortment', 'IN', $ids)->setField('good_status', $num);
            Db::commit();
            $res = 1;
        } catch(\Exception $e) {
            Db::rollback();
            $res = 0;
        }
        if($res == 1) {
            return json(['code' => 200, 'msg' => $msg]);
        } else {
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 所属商品列表
     * @return mixed|\think\response\Json
     */
    public function goodsList()
    {
        if(request()->isAjax()) {
            $where['good_assortment'] = session('assortment_id');
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('goods')->where($where)->count();// 获取总条数
            $lists = Db::name('goods')
                ->field('good_id, good_name, good_price, good_thumb, good_status, good_stock, good_coding, 
                good_weight, good_cost, good_type')
                ->where($where)
                ->page($Nowpage, $limits)
                ->order('good_create_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $assortment_id = input('id');
        if($assortment_id != ''){
            session('assortment_id', $assortment_id);
        }
        return $this->fetch('goodscate/goodslist');
    }
}